package com.fengba.nanzhi.tabbar01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.myfengba.MyCF;
import com.fengba.nanzhi.myinfo.MyInfo;
import com.fengba.nanzhi.myinfo.WxImg;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by SpakeBrony on 2018/4/22.
 */

public class FX_FD  extends Activity {

    private OKManager manager = OKManager.getInstance();

    private String credits;

    private  TextView mfdNumber;

    public FX_FD() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_fd);



        /**
         * 返回
         */
        ImageButton FFim01 = (ImageButton)findViewById(R.id.FFim01);
        FFim01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        /**
         * 索取
         */
        ImageButton FFiv02 = (ImageButton)findViewById(R.id.FFiv02);
        FFiv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FX_FD.this,FX_SQ.class);
                intent.putExtra("type","1");
                startActivity(intent);
            }
        });


        /**
         * 获取更多
         */
        ImageButton FFiv03 = (ImageButton)findViewById(R.id.FFiv03);
        FFiv03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FX_FD.this,com.fengba.nanzhi.fengba.MainActivity.class);
                intent.putExtra("id","3");
                startActivity(intent);
            }
        });

        /**
         * 疯豆明细
         */
        TextView FFtv01 = (TextView)findViewById(R.id.FFtv01);
        FFtv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FX_FD.this,FX_FD_Detailed.class);
                startActivity(intent);
            }
        });

        /**
         * 赠送
         */
        ImageButton FFiv04 = (ImageButton)findViewById(R.id.FFiv04);
        FFiv04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FX_FD.this,FX_SQ.class);
                intent.putExtra("type","2");
                startActivity(intent);
            }
        });

        TextView FFtv02 = (TextView)findViewById(R.id.FFtv02);
        FFtv02.setBackgroundResource(R.color.my_fd);

        mfdNumber= (TextView)findViewById(R.id.fdNumber);
        init();
        final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.FFrefreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                        if (credits == null||credits.equals("")) {
                            Toast.makeText(FX_FD.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        } else {
                            Toast.makeText(FX_FD.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        }
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

    }//都在这里打


    public void init ()
    {
        String id = null;
        try {
            Object user_info = Preferences.getParam(FX_FD.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        manager.type2005(id,new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        credits =jsonObject.get("data").toString();
                         mfdNumber.setText(credits);
                    } else {
                        Toast.makeText(FX_FD.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                Toast.makeText(FX_FD.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }



}
